<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="工程类型" prop="sortId">
        <el-select v-model="queryParams.sortId" placeholder="请选择工程分类" clearable size="small">
          <el-option v-for="sort in sortList" :key="sort.id" :label="sort.name" :value="sort.id" />
        </el-select>
      </el-form-item>

      <el-form-item label="任务状态" prop="zt">
        <el-select v-model="queryParams.zt" placeholder="请选择任务状态" clearable size="small">
          <el-option v-for="sort in ztList" :key="sort.id" :label="sort.name" :value="sort.id" />
        </el-select>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['tender:task:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['tender:task:edit']"
        >修改</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
     <el-table-column label="序号" type="index" align="center" show-overflow-tooltip width="50px"/>
      <el-table-column label="id" align="center" prop="id" v-if="false" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="任务内容" align="center" prop="taskContent" />
      <el-table-column label="完成时间" align="center" prop="finshDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finshDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工程分类" align="center" prop="sortId" :formatter="handphaseSort" />
      <el-table-column label="任务状态" align="center" prop="zt" :formatter="handphaseZt" />
      <el-table-column label="其他" align="center" prop="other" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="creatorDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creatorDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="300" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['tender:task:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['tender:task:remove']"
          >删除</el-button>
          
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改招标任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务内容" prop="taskContent">
          <el-input v-model="form.taskContent" placeholder="请输入任务内容" />
        </el-form-item>
        <el-form-item label="完成时间" prop="finshDate">
          <el-date-picker
            clearable
            size="small"
            style="width: 200px"
            v-model="form.finshDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择完成时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="工程分类" prop="sortId">
          <el-select v-model="form.sortId" placeholder="请选择工程分类" clearable size="small">
            <el-option v-for="sort in sortList" :key="sort.id" :label="sort.name" :value="sort.id" />
          </el-select>
        </el-form-item>

  <el-form-item label="工程地点" prop="address">
          <el-input v-model="form.address" placeholder="请输入工程地点" />
        </el-form-item>

        <el-form-item label="工程面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入工程面积" />
        </el-form-item>
     
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProjectTask, getProjectTask, delProjectTask, addProjectTask, updateProjectTask, exportProjectTask } from "@/api/tender/projectTask";

import { listSortType } from "@/api/tender/projectSort";
export default {
  name: "Task",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 招标任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: undefined,
        taskContent: undefined,
        finshDate: undefined,
        sortId: undefined,
        other: undefined,
        creatorId: undefined,
        creatorDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      sortList: [], //工程分类列表
      ztList: [
        { id: 1, name: "待评标" },
        { id: 2, name: "评标中" },
        { id: 3, name: "已完成" }
      ]
    };
  },
  created() {
    this.getList();
    this.getSortList();
  },
  methods: {
    /** 查询招标任务列表 */
    getList() {
      this.loading = true;
      listProjectTask(this.queryParams).then(response => {
        this.taskList = response.data.data.list;
        this.total = response.data.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        taskName: undefined,
        taskContent: undefined,
        finshDate: undefined,
        sortId: undefined,
        zt: undefined,
        address: undefined,
        area: undefined,
        remark: undefined,
        creatorId: undefined,
        creatorDate: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加招标任务";
      this.getSortList();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getProjectTask(id).then(response => {
        this.form = response.data.data;
        this.open = true;
        this.title = "修改招标任务";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateProjectTask(this.form).then(response => {
               if (response.data.code === '0') {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.data.msg);
              }
            });
          } else {
            addProjectTask(this.form).then(response => {
               if (response.data.code === '0') {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.data.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if(row.zt!=1){
        this.msgError("当前任务已启动评标，不允许删除。");
        return;
      }
      this.$confirm(
        '是否确认删除招标任务编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delProjectTask(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有招标任务数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportProjectTask(queryParams);
        })
        .then(response => {
          this.download(response.data.msg);
        })
        .catch(function() {});
    },
    getSortList() {
      const params = { type: 1 };
      listSortType(params).then(response => {
        this.sortList = response.data.data;
      });
    },
  
    handphaseSort(row, column) {
      for (var i = 0; i < this.sortList.length; i++) {
        if (row.sortId == this.sortList[i].id) {
          return this.sortList[i].name;
        }
      }
    },
    handphaseZt(row, column) {
      for (var i = 0; i < this.ztList.length; i++) {
        if (row.zt == this.ztList[i].id) {
          return this.ztList[i].name;
        }
      }
    },
    // handleCompany(row){
    //   //路由跳转
    //   this.$router.push({path:'/projectTask/type/data/'+row.id+''});
    // }
  }
};
</script>